<?php

/* 
 * Ejemplo : Escalado y rotacion de 2 superficies 
 * (Adaptado del ejemplo mini escalado por Hugo Ruscitti).
 * 
 * Copyright (C) 2006 Lucas Liendo.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

define('pi2', 6.28318);

function iniciar_sdl(&$pantalla)
{
	if (SDL_Init (SDL_INIT_VIDEO) == -1) 
	{
		printf("Error: %s\n", SDL_GetError());
		return 1;
	}

	$pantalla = SDL_SetVideoMode (300, 300, 16, SDL_SWSURFACE);
	
	if (is_null($pantalla))
	{
		printf("Error: %s\n", SDL_GetError());
		return 1;
	}
	
	return 0;
}

function limpiar($screen, $x, $y, $w, $h)
{
	$rect = array(
		'x' => $x,
		'y' => $y,
		'w' => $w,
		'h' => $h
	);
	SDL_FillRect($screen, &$rect, SDL_MapRGB($screen['format'], 20, 100, 20));
}


function inf_loop (&$ventana)
{
	$salir = 0;
	$angulo = 0;
	$zoom = 1;

	$imagen = IMG_Load ("ima/mail.png");
	$imagen2 = IMG_Load ("ima/stop.png");

	$rect1['x'] = 150;
	$rect1['y'] = 150;
	$rect1['w'] = $rect1['h'] = 100;

	$rect2['x'] = 100;
	$rect2['y'] = 100;
	$rect2['w'] = $rect2['h'] = 100;

	while (!$salir)
	{
		echo '.';
		while (SDL_PollEvent (&$evento))
		{
			switch ($evento['type'])
			{
				case SDLK_q :
					$salir = 1;
					break;

				case SDL_QUIT :
					$salir = 1;
					break;

				default :
					break;
			}
		}

		if ($angulo < 360)
		{
			$angulo = $angulo + 1;
			$zoom = ($angulo * pi2) / 360;
			$imagen_copia = gfx_rotozoomSurface ($imagen['handle'], $angulo, abs (sin ($zoom)), 1);
			SDL_BlitSurface($imagen_copia, null, $ventana, null);
			$imagen2_copia = gfx_rotozoomSurface ($imagen2['handle'], $angulo, abs (cos ($zoom)), 1);

			$rect1['x'] = 190 - $imagen_copia['w'] / 2;
			$rect1['y'] = 150 - $imagen_copia['h'] / 2;
			$rect1['w'] = $rect1['h'] = 100;

			$rect2['x'] = 110 - $imagen2_copia['w'] / 2;
			$rect2['y'] = 150 - $imagen2_copia['h'] / 2;
			$rect2['w'] = $rect2['h'] = 100;

			limpiar($ventana, 0, 0, $ventana['w'], $ventana['h']);
			SDL_BlitSurface ($imagen_copia, NULL, $ventana, &$rect1);
			SDL_BlitSurface ($imagen2_copia, NULL, $ventana, &$rect2);
			SDL_FreeSurface ($imagen_copia);
			SDL_FreeSurface ($imagen2_copia);
			SDL_Flip ($ventana);
		}
		else
			$angulo = 0;
		
		SDL_Delay (10);
	}

	SDL_FreeSurface ($imagen);
	SDL_FreeSurface ($imagen2);
}


if (iniciar_sdl ($ventana))
	return 1;

inf_loop ($ventana);

SDL_Quit ();

?>
